﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace Mario_Map_Editor
{
    public partial class frmMain : Form
    {
        public static int columns;
        public static int rows;
        private PictureBox[,] arrPictureBox;
        private int[] Id = new int [22];
        private int[,] matrix;

        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            LoadImages();
            if (columns != 0 && rows != 0)
            {
                CreateMatrix();
            }
        }

        private void LoadImages()
        {
            string[] Files = Directory.GetFiles("Images");
            thumbnailsFLP.Controls.Clear();
            int i = 0;
            foreach (String image in Files)
            {
                if (image.ToLower().EndsWith(".jpg") || image.ToLower().EndsWith(".GIF") ||
                    image.ToLower().EndsWith(".png") || image.ToLower().EndsWith(".bmp") ||
                    image.ToLower().EndsWith(".jpeg"))
                {
                    PictureBox display = new PictureBox();
                    display.Name = (i+1).ToString();
                    display.SizeMode = PictureBoxSizeMode.StretchImage;
                    display.Image = Image.FromFile(image);
                    display.Height = 62;
                    display.Width = 62;
                    display.Cursor = Cursors.Hand;
                    thumbnailsFLP.Controls.Add(display);
                    display.Click += new EventHandler(pic_Click);
                }
                if (i++ == 4)
                    Application.DoEvents();
            }
        }

        void pic_Click(object sender, EventArgs e)
        {
            PictureBox pic = (PictureBox)sender;
            lblID.Text = pic.Name;
            pictureBox.Image = pic.Image;
            pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox.Height = 90;
            pictureBox.Width = 90;
        }

        void CreateMatrix()
        {
            matrix = new int[rows, columns];
            arrPictureBox = new PictureBox[rows, columns];
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    matrix[i, j] = 0;
                    string num = i.ToString() + " " + j.ToString();
                    arrPictureBox[i, j] = new PictureBox();
                    arrPictureBox[i, j].Name = num;
                    arrPictureBox[i, j].Size = new Size(40, 40);
                    arrPictureBox[i, j].Location = new Point(j * 40, i * 40);
                    arrPictureBox[i, j].BorderStyle = BorderStyle.FixedSingle;
                    arrPictureBox[i, j].Cursor = Cursors.Hand;
                    arrPictureBox[i, j].Click += new EventHandler(arrPictureBox_Click);
                    panel1.Controls.Add(arrPictureBox[i, j]);
                }
            }
        }

        void arrPictureBox_Click(object sender, EventArgs e)
        {
            PictureBox pic = (PictureBox)sender;
            String[] split = pic.Name.Split(' ');
            int i = Convert.ToInt32(split[0]);
            int j = Convert.ToInt32(split[1]);
            matrix[i, j] = Convert.ToInt32(lblID.Text);
            pic.Image = pictureBox.Image;
            pic.Height = 40;
            pic.Width = 40; 
            pic.SizeMode = PictureBoxSizeMode.StretchImage;
        }

        private void btnNewMap_Click(object sender, EventArgs e)
        {
            if (arrPictureBox != null)
            {
                for (int i = 0; i < rows; i++)
                {
                    for (int j = 0; j < columns; j++)
                    {
                        arrPictureBox[i, j].Dispose();
                        arrPictureBox[i, j] = null;
                    }
                }
            }

            columns = 0;
            rows = 0;
            
            frmNewMap newmap = new frmNewMap();
            DialogResult result = newmap.ShowDialog();

            if (result == DialogResult.Cancel)
            {
                frmMain_Load(sender, e);
            }
        }

        private void btnSaveMap_Click(object sender, EventArgs e)
        {
            FileInfo Myfile = new FileInfo("D:\\Text.txt");
            StreamWriter wri = Myfile.CreateText();
            String matrixtxt = rows.ToString() + " " + columns.ToString() + "\r\n";
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    if (matrix[i, j] < 10)
                    {
                        matrixtxt += "0" + matrix[i, j].ToString() + " ";
                    }
                    else
                    {
                        matrixtxt += matrix[i, j].ToString() + " ";
                    }
                }
                matrixtxt += "\r\n";
            }
            wri.Write(matrixtxt);
            wri.Close();
        }

        private void btnLoadMap_Click(object sender, EventArgs e)
        {

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void panel1_Resize(object sender, EventArgs e)
        {
            Invalidate();
        }
    }
}
